<template>
  <div class="bgw">
    <el-card shadow="never">
      <el-form ref="form" class="box-card" :inline="true" :model="form">
        <div class="formtop">
          <el-form-item label="周期">
            <el-radio-group v-model="dateType" size="mini">
              <el-radio-button label="日" />
              <el-radio-button label="月" />
              <el-radio-button label="年" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="日期">
            <el-date-picker
              ref="picker"
              v-model="form.end_time"
              size="mini"
              :type="parseDate()"
              placeholder="选择日期"
              style="width: 100%"
            />
          </el-form-item>
          <el-form-item label="" label-width="0">
            <el-button
              class="h50"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="getChartData"
            />
          </el-form-item>
          <el-form-item label="" label-width="0">
            <!-- tab 类型 -->
            <div class="formRight">
              <template v-for="item in tabData">
                <!-- eslint-disable-next-line -->
                <div
                  style="cursor: pointer"
                  :class="item.id === form.type_id ? 'active' : ''"
                  @click="handleType(item.id)"
                >
                  {{ item.type_name }}
                </div>
              </template>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
    <div class="mp30">
      <!-- 顶部提示 -->
      <div class="topData">
        <div>
          累计警告: <span>{{ list.count }}</span>
        </div>
        <div>
          同比: <span>{{ list.rate1 }}</span>
        </div>
        <div>
          环比: <span>{{ list.rate2 }}</span>
        </div>
        <div>
          安全评估: <span>{{ list.assess }}</span>
        </div>
      </div>
      <!-- 柱状图 -->
      <div id="myChart1" class="chart" />
      <!-- 饼状图 -->
      <!-- <div class="radius">
        <div id="myChart2" />
        <div id="myChart3" />
        <div id="myChart4" />
        <div id="myChart5" />
      </div> -->
      <!--  表格数据  -->
      <div class="tableBox">
        <el-table
          :data="tableData"
          border
          style="width: 100%; border-radius: 10px"
        >
          <el-table-column prop="station_name" label="站点" align="center" />
          <el-table-column prop="name" label="设备" align="center" />
          <el-table-column prop="count" label="数量" align="center" />
          <el-table-column prop="count" label="遥信" align="center" />
          <el-table-column prop="count" label="遥测" align="center" />
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import { electricityParameters, monitorChartData ,policeStatistics} from '@/api/realTime.js'

export default {
  name: 'content',
  components: {},
  props: {
    choseData: {
      type: Object,
      required: {},
    },
  },
  data() {
    return {
      dateType: '日', // 日期得格式
      tabData: [], // tab数据
      typeId: null, // tabId
      tableData: [],
      options: [],
      form: {
        end_time: '',
        monitor_id: '',
        param_id: [],
        type_id: null,
      },
      list: [],
      //  图表数据
      option: {
        title: {
          text: '',
        },
        tooltip: {
          trigger: 'axis',
        },
        legend: {},
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: 'none',
            },
            dataView: { readOnly: false },
            magicType: { type: ['bar', 'line'] },
            restore: {},
            saveAsImage: {
              show: true,
              icon: 'M4.7,22.9L29.3,45.5L54.7,23.4M4.6,43.6L4.6,58L53.8,58L53.8,43.6M29.2,45.1L29.2,0',
              title: '保存为图片',
              type: 'png',
              connectedBackgroundColor: '#fff',
              name: '',
              excludeComponents: ['toolbox'],
              lang: ['右键另存为图片'],
            },
          },
        },
        axisPointer: {
          link: [
            {
              xAxisIndex: 'all',
            },
          ],
        },
        dataZoom: [],
        xAxis: {
          type: 'category',
          // boundaryGap: false,
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: '{value} °C',
          },
        },
        series: [
          {
            name: 'Highest',
            type: 'bar',
            barWidth: 20,
            data: [10, 11, 13, 11, 12, 12, 9],
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' },
              ],
            },
          },
        ],
      },
      option1: {
        tooltip: {
          trigger: 'item',
        },
        legend: {
          top: '5%',
          left: 'center',
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: [{ value: 1048, name: 'Search Engine' }],
          },
        ],
      },
      option2: {
        tooltip: {
          trigger: 'item',
        },
        legend: {
          top: '5%',
          left: 'center',
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 1048, name: 'Search Engine' },
              { value: 735, name: 'Direct' },
            ],
          },
        ],
      },
      option3: {
        tooltip: {
          trigger: 'item',
        },
        legend: {
          top: '5%',
          left: 'center',
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 1048, name: 'Search Engine' },
              { value: 735, name: 'Direct' },
            ],
          },
        ],
      },
      option4: {
        tooltip: {
          trigger: 'item',
        },
        legend: {
          top: '5%',
          left: 'center',
        },
        series: [
          {
            name: 'Access From',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2,
            },
            label: {
              show: false,
              position: 'center',
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold',
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 1048, name: 'Search Engine' },
              { value: 735, name: 'Direct' },
            ],
          },
        ],
      },
    }
  },
  watch: {
    // 树形数据
    choseData(nVal, oVal) {
      this.form.monitor_id = nVal.id
      // this.getTopSearch()
      this.getChartData()
    },
    dateType(nVal, oVal) {
      this.parseDate()

      this.getChartData()
    },
  },
  created() {
    // 获取当前时间
    this.form.end_time = this.getDay(0)
  },
  mounted() {
    console.log('接受的站点参数', this.choseData)
    this.drawLine()
    this.getChartData()
  },
  methods: {
    // 解析格式
    parseDate() {
      return this.dateType === '日'
        ? 'date'
        : this.dateType === '月'
        ? 'month'
        : 'year'
    },
    //   获取图表数据
    getChartData() {
      const data = {}
      data.day_time = this.form.end_time
      if (this.dateType == '日') {
        data.period_type = 'day2'
      } else if (this.dateType == '月') {
        data.period_type = 'month'
        // data.day_time = data.day_time.substr(0, 7)
      } else {
        data.period_type = 'year'
        // data.day_time = data.day_time.substr(0, 4)
      }

      data.station_id = this.choseData.id
      const myChart1 = this.$echarts.init(document.getElementById('myChart1'))
      policeStatistics(data).then((res) => {
        this.list = res.data
        this.option.xAxis.data = res.data.xData
         this.option.color=['#409EFF']
        this.option.series[0].data = res.data.series.data
        this.tableData = res.data.table
        myChart1.setOption(this.option, true)
      })
    },
    // 获取顶部参数查询
    getTopSearch() {
      electricityParameters(this.choseData.id, 0).then((res) => {
        this.tabData = res.data.data
        this.options = res.data.child
        if (this.tabData.length > 0) {
          this.form.type_id = this.tabData[0].id
          this.getChartData()
        }
      })
    },
    // 初始化表格
    drawLine() {
      // 基于准备好的dom，初始化echarts实例
      // const myChart1 = this.$echarts.init(document.getElementById('myChart1'))
      // const myChart2 = this.$echarts.init(document.getElementById('myChart2'))
      // const myChart3 = this.$echarts.init(document.getElementById('myChart3'))
      // const myChart4 = this.$echarts.init(document.getElementById('myChart4'))
      // const myChart5 = this.$echarts.init(document.getElementById('myChart5'))
      // 绘制图表
      // myChart1.setOption(this.option, true)
      // myChart2.setOption(this.option1, true)
      // myChart3.setOption(this.option2, true)
      // myChart4.setOption(this.option3, true)
      // myChart5.setOption(this.option4, true)
    },
    /**
     * @description: 获取当前日期
     * @author: hst
     * @param: day: 0今天，-1昨天
     */
    getDay(day) {
      var today = new Date()
      var targetday = today.getTime() + 1000 * 60 * 60 * 24 * day
      today.setTime(targetday)
      var tYear = today.getFullYear()
      var tMonth = today.getMonth()
      var tDate = today.getDate()
      tMonth = this.doHandleMonth(tMonth + 1)
      tDate = this.doHandleMonth(tDate)
      return tYear + '-' + tMonth + '-' + tDate
    },
    doHandleMonth(month) {
      var m = month
      if (month.toString().length === 1) {
        m = '0' + month
      }
      return m
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .tableHead {
  background-color: #edf1f8 !important;
}
.bgw {
  background-color: #fff;
}
.mp30 {
  padding: 30px;
}
.h50 {
  height: 28px;
}
.box-card {
  height: 30px;
}
.topData {
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  width: 100%;
  height: 45px;
  border-radius: 10px;
  border: 1px solid #e4e4e4;
}
.formtop {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  height: 50px;
}
.formRight {
  overflow-x: auto;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  div {
    height: 35px;
    margin: 0 10px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #838ea0;
  }
  .active {
    height: 35px;
    background: linear-gradient(90deg, #3494ff, #006dff);
    box-shadow: 0px 6px 12px 0px rgba(35, 113, 255, 0.29);
    border-radius: 17px;
    color: #ffffff;
    padding: 0px 25px;
  }
}
.chart {
  width: 100%;
  height: 364px;
  border: 1px solid #e4e4e4;
  border-radius: 10px;
  overflow: hidden;
}
.radius {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 333px;
  border-radius: 10px;
  border: 1px solid #e4e4e4;
  div {
    flex: 1;
    height: 100%;
  }
}
.tableBox {
  width: 100%;
  margin: 20px auto;
}
.warningBox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: 1140px;
  height: 54px;
  border: 1px solid #cecdcd;
  background: #f1f1f1;
  border-radius: 10px;
  font-size: 14px;
  padding: 15px;
  margin: 10px auto;
  .left {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    background-color: #333333;
    margin-right: 10px;
  }
  .right {
    font-size: 14px;
  }
}
</style>
